package com.zxm.service.impl;
/**
 * @author 张鑫铭
 */
import com.alibaba.dubbo.config.annotation.Service;
import com.zxm.mapper.ContentCategoryMapper;
import com.zxm.mapper.UserBodyInfoMapper;
import com.zxm.mapper.UserInfoMapper;
import com.zxm.pojo.UserBodyInfo;
import com.zxm.pojo.UserBodyInfoExample;
import com.zxm.pojo.UserBodyInfoExample.Criteria;
import com.zxm.pojo.UserInfo;
import com.zxm.pojo.UserInfoExample;
import com.zxm.service.UserBodyInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service(interfaceName="com.zxm.service.UserBodyInfoService")
@Transactional
public class UserBodyInfoServiceImpl implements UserBodyInfoService {
    @Autowired
    private UserBodyInfoMapper userBodyInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    private static final Logger logger = LoggerFactory.getLogger(ContentCategoryServiceImpl.class);
    
    //显示用户身体信息
    @Override
    public List<UserBodyInfo> getAll(String username,Date start ,Date end) {
    	UserInfoExample ue=new UserInfoExample();
    	UserInfoExample.Criteria c_userInfo=ue.createCriteria();
    	c_userInfo.andUsernameEqualTo(username);
    	List<UserInfo> userInfoList = userInfoMapper.selectByExample(ue);
		UserInfo userInfo = null;
    	if(null!=userInfoList && userInfoList.size()>0){
    		userInfo = userInfoList.get(0);
		}
    	UserBodyInfoExample ube=new UserBodyInfoExample();
    	Criteria c=ube.createCriteria();
    	//当结束时间为空时，返回值为起始时间到今天的值
		if(null!=userInfo){
			if(end==null) {
				c.andUserBodyTimeGreaterThanOrEqualTo(start).andIdEqualTo(userInfo.getId());
				return this.userBodyInfoMapper.selectByExample(ube);
			}
			c.andUserBodyTimeBetween(start, end).andIdEqualTo(userInfo.getId());
		}
		return this.userBodyInfoMapper.selectByExample(ube);
    }

	//增加用户身体数据
	@Override
	public void insertUserBodyInfo(UserBodyInfo userBodyInfo,String username) {
		UserInfoExample ue=new UserInfoExample();
		com.zxm.pojo.UserInfoExample.Criteria c_userInfo=ue.createCriteria();
		c_userInfo.andUsernameEqualTo(username);
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(ue);
		UserInfo userInfo= null;
		if(null != userInfoList && userInfoList.size()>0){
			userInfo = userInfoList.get(0);
			userBodyInfo.setUserid(userInfo.getId().intValue());
			this.userBodyInfoMapper.insert(userBodyInfo);
		}

	}

	//根据传来的username到userinfo中去找到对应的id，根据id再去找到bmi
	@Override
	public UserBodyInfo getUserBodyInfoBMI(String username) {
		UserInfoExample ue=new UserInfoExample();
    	UserInfoExample.Criteria c_userInfo=ue.createCriteria();
    	c_userInfo.andUsernameEqualTo(username);
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(ue);
		UserInfo userInfo= null;
		if(null != userInfoList && userInfoList.size()>0){
			userInfo=userInfoList.get(0);
			UserBodyInfo userBodyInfo =userBodyInfoMapper.selectByPrimaryKey(userInfo.getId());
			return userBodyInfo;
		}
		return null;
	}

	//根据用户ID来找到用户的最新的身体数据
	@Override
	public UserBodyInfo selectUserBodyInfoByUserID(Integer userid) {
		UserBodyInfo userBodyInfo=null;
		try {
			userBodyInfo =userBodyInfoMapper.selectByNewUserBodyInfo(userid);
		}catch (Exception e) {
			return null;
		}
		return userBodyInfo;
	}
}
